package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏信息业务，参数：{}",geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        //之前没有对围栏的车辆数据进行处理,现在需要对数据进行补充:
        /* 遍历每一个围栏,依次为每个围栏补充车辆数据 totalNum availableNum vehicleList*/
        for(int i = 0; i < list.size(); i++){
            //1.遍历刚刚查出来的围栏集合,获取当前遍历到的围栏对象
            GeofenceVO geofenceVO = list.get(i);
            //2.封装车辆的查询对象,条件是当前围栏的id
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            //3.调用车辆数据层,根据上方的vehicleQuery查询车辆列表数据
            List<VehicleVO>  vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //4.获取当前围栏上绑定的车辆总数=>查出来的车辆集合size
            int totalNum = vehicleVOList.size();
            //5.定义变量用来保存可分配车辆数
            int availableNum = 0;
            //6.遍历刚刚查出来的车辆集合,判断当前车辆的状态
            for(VehicleVO vehicleVO : vehicleVOList){
                //7.判断当前遍历到的车是否为空闲状态,如果是空闲状态,可用车辆数+1
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            //8.给围栏对象设置车辆数据
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            //此处的车辆列表是供后续分配车辆时查看车辆详情时使用
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("保存电子围栏信息业务，参数：{}",geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        if(geofence.getId() != null){//修改
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }else{//新增
            geofence.setStatus("1");//每个围栏新增时都是启用状态
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除电子围栏信息业务，参数：{}",id);
        //业务:需要判断当前围栏上是否有已绑定的车辆,如果有绑定车辆,就不能删除!
        //1.封装车辆查询的DTO层对象VehicleQuery
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.调用车辆数据层查询是否有绑定在当前围栏上的车辆数据
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //3.如果当前围栏上有绑定车辆,业务层可以抛出操作失败的异常
        if(list.size()>0 && list != null){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else{
            //4.如果该围栏下没有绑定车辆,可以正常删除
            geofenceMapper.deleteGeofence(id);
        }
    }
}






